package com.bwf.file;

import java.io.FileReader;
import java.io.IOException;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/17 10:52
 * @Copyright 博为峰
 */
public class FileReaderDemo {
    public static void main(String[] args) {
//        ctrl + alt + t
//        m1();
//        m2();
//        m3();
        /*
          public int read() 读取的字符
          public int read(char[] cbuf) 读取的字符数，如果已到达流的末尾，则返回 -1

          String(char[] value, int offset, int count)

         */
        char[] chars = new char[1024];
        try (FileReader fileReader = new FileReader("fw01.txt")) {
            int r = 0;
            //r = fileReader.read(chars)
            //(r = fileReader.read(chars))
            //r = fileReader.read(chars) != -1
            while ((r = fileReader.read(chars)) != -1) {
                String s = new String(chars, 0, r);//变成字符串
                System.out.println(s);
            }
            /*while (r != -1) {
                r = fileReader.read(chars);
                if(r != -1){
                    String s = new String(chars, 0, r);//变成字符串
                    System.out.println(s);
                }
            }*/
        } catch (IOException e) {

        }
    }

    private static void m3() {
        char[] chars = new char[3];
        try (FileReader fileReader = new FileReader("fw01.txt")) {
            fileReader.read(chars);
            String s = new String(chars);
            System.out.println(s);
        } catch (IOException e) {

        }
    }

    private static void m2() {
        try (FileReader fileReader = new FileReader("fw01.txt")) {
            int r = 0;
            while ((r = fileReader.read()) != -1) {
                System.out.println((char) r);
            }
        } catch (IOException e) {

        }
    }

    private static void m1() {
        try (FileReader fileReader = new FileReader("fw01.txt")) {
            int r1 = fileReader.read();
            System.out.println((char) r1);
            int r2 = fileReader.read();
            System.out.println((char) r2);
            int r3 = fileReader.read();
            System.out.println((char) r3);
            int r4 = fileReader.read();
            System.out.println(r4);
        } catch (IOException e) {

        }
    }
}
